package com.web.music.model;




/*import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;*/

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@Entity

@NamedQueries({
		@NamedQuery(name="findTracksByName",
				query="SELECT t FROM Track t WHERE t.title LIKE :trackName"),
				
		@NamedQuery(name="findTracksByArtistId",
				query="")
		/*		
		SELECT * FROM Track WHERE Album.artist_id = :artistId  	
		 JOIN Album ON (Album.id=Track.album_id)
		 
		*/		

} )
public class Track implements PersistentObject {

	@Id @GeneratedValue
	private Long id;
	
	private String title;
	
	private int num;
	
	private String mp3;
	
	@Column(columnDefinition="MEDIUMBLOB")
	private byte[] content = null;
	
	@ManyToOne
	private Album album = null;

	public Track() {
		super();
	}
	
	public Track(String title, int num, String mp3) {
		super();
		this.num = num;
		this.title = title;
		this.mp3 = mp3;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}
	
	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	public void setContentFromFile(String filename) {
	/*	try {
			File file = new File(filename);
			FileInputStream fis = new FileInputStream(file);
			
			byte[] result = new byte[(int) file.length()];
			fis.read(result);
			this.content = result;
			fis.close();
		} catch (FileNotFoundException e) {
			this.content = null;
		} catch (IOException e) {
			this.content = null;
		} catch (Exception e) {
		} */
	}

	public void setId(Long id) {
		this.id = id;
	}


	public Long getId() {		
		return id;
	}
	
	public void setNum(int num) {
		this.num = num;
	}
	
	public int getNum() {
		return num;
	}
	
	public String getMp3() {
		return mp3;
	}

	public void setMp3(String mp3) {
		this.mp3 = mp3;
	}

	@Override
	public boolean equals(Object obj) {
		boolean result = true;
		if (obj instanceof Track) {
			
			result = this.num == ((Track) obj).getNum()
						&& this.title.equals(((Track) obj).getTitle())
						&& this.mp3.equals(((Track) obj).getMp3());
			return result;
		}
		return false;
	}


	@Override
	public String toString() {
		return this.id + " " + this.num + " " + this.title + " " + this.mp3;
	}
	
	@Override
	public int hashCode() {
		return toString().hashCode();
	}
	
}
